<?php
class CParam {
	private $route_config = 'd';
	public function __construct(){
		if(!get_magic_quotes_gpc()){
			$request = new SlashesValidator();
			$_POST = $request->new_addslashes($_POST);
			$_GET = $request->new_addslashes($_GET);
			$_REQUEST = $request->new_addslashes($_REQUEST);
			$_COOKIE = $request->new_addslashes($_COOKIE);
		}
		$this->route_config = RonBase::load_config('route','default');
		if(isset($_GET['page'])) $_GET['page'] = max(intval($_GET['page']),1);
		return true;
	}
	
	/**
	 * 获取模型
	 */
	public function route_m() {
		$m = isset($_GET['m']) && !empty($_GET['m']) ? $_GET['m'] : (isset($_POST['m']) && !empty($_POST['m']) ? $_POST['m'] : '');
		if (empty($m)) {
			return $this->route_config['m'];
		} else {
			return $m;
		}
	}

	/**
	 * 获取控制器
	 */
	public function route_c() {
		$c = isset($_GET['c']) && !empty($_GET['c']) ? $_GET['c'] : (isset($_POST['c']) && !empty($_POST['c']) ? $_POST['c'] : '');
		if (empty($c)) {
			return $this->route_config['c'];
		} else {
			return $c;
		}
	}

	/**
	 * 获取事件
	 */
	public function route_a() {
		$a = isset($_GET['a']) && !empty($_GET['a']) ? $_GET['a'] : (isset($_POST['a']) && !empty($_POST['a']) ? $_POST['a'] : '');
		if (empty($a)) {
			return $this->route_config['a'];
		} else {
			return $a;
		}
	}
	
	/**
	 * 设置 cookie
	 * @param string $var     变量名
	 * @param string $value   变量值
	 * @param int $time    过期时间
	 */
	public function set_cookie($var, $value = '', $time = 0){
		$time = $time > 0 ? $time : ($value == '' ? SYS_TIME - 3600 : 0);
		$var = RonBase::load_config('system','cookie_pre').$var;
		$_COOKIE[$var] = $value;
		$process = new CProcess();
		if(is_array($value)){
			foreach ($value as $k => $v) {
				setcookie($var.'['.$k.']',$process->sys_auth($v),$time);
			}
		} else {
			setcookie($var,$process->sys_auth($value),$time);		
		}
	}
	
	/**
	 * 获取通过 set_cookie 设置的 cookie 变量 
	 * @param string $var 变量名
	 * @return mixed 成功则返回cookie 值，否则返回 false
	 */
	public static function get_cookie($var){
		$process = new CProcess();
		$var = RonBase::load_config('system','cookie_pre').$var;
		return isset($_COOKIE[$var]) ? $process->sys_auth($_COOKIE[$var], 'DECODE') : '';
	}
	
	
}